--[[
   Copyright (c) 2012 TPV R&D Center Brazil
   All rights reserved
 
   FileName: app.time
 
   Created by: Guilherme Guimarães
   Date: 08/16/2012
--]]
------------------------------------------------------------------------
local tCauses = {}
tCauses['advice'] = false
tCauses['osd'] = false
tCauses['app'] = false
local bIsCCEnabled = false
local bIsCCOnMute = false
--module table
--Time module, responsible of time, date, sleep
local tCaptionApp = {}
------------------------------------------------------------------------
--module functions

--- Synchronous function to get the currently showing caption.
-- If no caption is showing, returns nil.
-- @return captionNumber number The number of the currently showing caption.
-- @return captionDescription string The description of the currently showing caption.
function tCaptionApp.getCurrentCaption()
   local tTempCaption = control.getCurrentCaption()
   local tCaption = {}
   if (tTempCaption) then
	  tCaption.nId = tTempCaption.id
	  tCaption.sDescription = tTempCaption.description
   else
      tCaption.nId = 0
   end
   return tCaption
end

--- Synchronous function to get the current channel's caption list.
-- @return table Array of caption objects, each consisting of a table containing the fields 'number' and 'description'.
-- @usage second_caption_description = control.getCaptionList()[2].description
function tCaptionApp.getCaptionList()
   local tTempCaptionList = control.getCaptionList()
   local tCaptionList = {}
   for nI = 1, #tTempCaptionList do
      tCaptionList[nI] = {}
      tCaptionList[nI].nId = tTempCaptionList[nI].id
      tCaptionList[nI].sDescription = tTempCaptionList[nI].description
   end
   return tCaptionList
end

--- Synchronous function to get the current channel's caption list.
-- @return table Array of caption objects, each consisting of a table containing the fields 'number' and 'description'.
-- @usage second_caption_description = control.getCaptionList()[2].description
function tCaptionApp.setCaptionOnMute(bCCOnMute)
   if (not bIsCCEnabled) then
      if (bCCOnMute) then
         local tCaptionList = control.getCaptionList()
         nRet = control.setCaption(tCaptionList[1])  
      else
         nRet = control.setCaption()  
      end      
   end
   bIsCCOnMute = bCCOnMute
end

--- Synchronous function to set the caption.
-- @param number number The number of the caption to show. Nil to disable captions.
-- @return boolean True if the command was sent successfully, false if some error occured.
function tCaptionApp.setCaption(nCaption)
   local nRet
   if (nCaption == nil) then
      nCaption = 0
   end
   if (nCaption > 0) then
      bIsCCEnabled = true
      local tCaptionList = control.getCaptionList()
      nRet = control.setCaption(tCaptionList[nCaption])
   else
      bIsCCEnabled = false
      control.muteCaption()
      nRet = control.setCaption()
   end
   return nRet
end

--- Synchronous function to set the caption.
-- @param number number The number of the caption to show. Nil to disable captions.
-- @return boolean True if the command was sent successfully, false if some error occured.
function tCaptionApp.muteCaption(bMute,sCause)
   local nRet
   if (bMute) then
      nRet = control.muteCaption()
   else
      if (sCause == 'app') then
         if (not (tCauses.osd or tCauses.advice)) then
            nRet = control.unmuteCaption()
         end
      elseif (sCause == 'advice') then
         if (not (tCauses.osd or tCauses.app)) then
            nRet = control.unmuteCaption()
         end
      else
         if (not (tCauses.app or tCauses.advice)) then
            nRet = control.unmuteCaption()
         end
      end
   end
   tCauses[sCause] = bMute
   return nRet
end
------------------------------------------------------------------------
return tCaptionApp
